import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    public int usedSize;
    Queue<Integer> qu1;
    Queue<Integer> qu2;
    public MyStack() {
        qu1=new LinkedList<>();
        qu2=new LinkedList<>();
    }
    //入栈
    public void push(int x) {
        if(!qu1.isEmpty()){
            qu1.offer(x);
        }else if(!qu2.isEmpty()){
            qu2.offer(x);
        }else{
            qu1.offer(x);
        }
        usedSize++;
    }
    //出栈
    public int pop() {
        if(empty()){
            return -1;
        }
        int size1=qu1.size();
        int size2=qu2.size();
        if(!qu1.isEmpty()){
            for(int i=0;i<size1-1;i++){
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        }else{
            for(int i=0;i<size2-1;i++){
                qu1.offer(qu2.poll());
            }
            return qu2.poll();
        }
    }
    //查看栈顶元素
    public int top() {
        if(empty()){
            return -1;
        }
        int size1=qu1.size();
        int size2=qu2.size();
        int tmp=0;
        if(!qu1.isEmpty()){
            for(int i=0;i<size1;i++){
                tmp=qu1.poll();
                qu2.offer(tmp);
            }
            return qu1.poll();
        }else{
            for(int i=0;i<size2;i++){
                tmp=qu2.poll();
                qu1.offer(tmp);
            }
            return tmp;
        }
    }
    public boolean empty() {
        return usedSize==0;
    }
}
